package main

import (
	"fmt"
	"git.oschina.net/fanbuchi/ini/config/etc"
	"github.com/go-ini/ini"
	"github.com/tencentyun/scf-go-lib/cloudfunction"
	"os"
)
//1. go get github.com/jteeuwen/go-bindata/...
//2. go-bindata  -pkg config -o config/bindata.go config/config/
//3. data,err:=etc.Asset("config/my.ini")
//4. cfg, err := ini.Load(data)

func main() {
	cloudfunction.Start(entry1)
}

func entry1() {
	//0 = LD_LIBRARY_PATH=/var/runtime/java8:/var/user
	//2 = HOSTNAME=c7efa232-6be5-11e9-b8ed-5254005dc76e
	//3 = SHELL=/bin/bash
	//4 = USER=qcloud
	//5 = USER_CODE_ROOT=/var/user/
	//6 = PATH=/usr/local/bin:/usr/bin/:/bin:/var/lang/bin
	//7 = _LAMBDA_SERVER_PORT=28902
	//8 = _=/var/runtime/go/bootstrap-go
	//9 = PWD=/
	//10 = NODE_PATH=/var/user:/var/user/node_modules:/var/lang/node6/lib/node_modules
	//11 = PYTHONDONTWRITEBYTECODE=x
	//13 = SHLVL=3
	//14 = HOME=/home/qcloud
	//15 = TENCENTCLOUD_RUNENV=SCF
	//16 = LOGNAME=qcloud
	//17 = PYTHONPATH=/var/user
	//18 = _SCF_SERVER_PORT=28902
	//19 = CLASSPATH=/var/runtime/java8:/var/runtime/java8/lib/*
	for i, env := range os.Environ() {
		fmt.Println(i, "=", env)
	}
	mode := os.Getenv("mode")
	fmt.Println("mode=>",mode)

	//old way
	data,err:=etc.Asset("config/ini/my.ini")
	//cfg, err := ini.Load("config/my.ini")
	cfg, err := ini.Load(data)
	if err != nil {
		fmt.Printf("Fail to read file: %v", err)
		os.Exit(1)
	}

	// 典型读取操作，默认分区可以使用空字符串表示
	fmt.Println("App Mode:", cfg.Section("").Key("app_mode").String())
	fmt.Println("Data Path:", cfg.Section("paths").Key("data").String())

	// 我们可以做一些候选值限制的操作
	fmt.Println("Server Protocol:",
		cfg.Section("server").Key("protocol").In("http", []string{"http", "https"}))
	// 如果读取的值不在候选列表内，则会回退使用提供的默认值
	fmt.Println("Email Protocol:",
		cfg.Section("server").Key("protocol").In("smtp", []string{"imap", "smtp"}))

	// 试一试自动类型转换
	fmt.Printf("Port Number: (%[1]T) %[1]d\n", cfg.Section("server").Key("http_port").MustInt(9999))
	fmt.Printf("Enforce Domain: (%[1]T) %[1]v\n", cfg.Section("server").Key("enforce_domain").MustBool(false))

	// 差不多了，修改某个值然后进行保存
	cfg.Section("").Key("app_mode").SetValue("production")
	cfg.SaveTo("my.ini.local")
}